package defaultPackage;
 
import java.awt.Graphics;

import taskMgr.TaskBuildTile;

public class Grid 
{
	public final static int dimensionx = 150;
	public final static int dimensiony = 150;
	public Tile tiles[][];
	MainClass main;
	private ObjectManager objectmanager;
	int count = 0;
	public Grid(MainClass main)
	{
		this.main = main;
		tiles = new Tile[dimensionx][dimensiony];
		for (int y = 0; y < dimensiony; y++)
		{
			for (int x = 0; x < dimensionx; x++)
			{
				tiles[x][y] = new Tile (x,y,tileType.Space,0,false);
			}
		}
	}
	public void setObjectmanager(ObjectManager om)
	{
		objectmanager = om;
	}
	public void setTileDesig(Designation designation,tileType type)
	{
		
		if (designation.isDesignating() && (Designation.designationArea *type.getVal()) <= Resources.getResources())
    	{		
			{
				if (designation.starty <= designation.endy && designation.startx <= designation.endx)
				{
					for (int y = (designation.starty)/10; y <= (designation.endy)/10;y++)
					{
						for (int x = (designation.startx)/10; x <= (designation.endx)/10;x++)
						{
								//setTiles(x,y,type);
								main.taskManager.addTask(new TaskBuildTile(type, x, y));
						}
					}
				}
    		
				else if (designation.starty >= designation.endy && designation.startx >= designation.endx)
				{
					for (int y = (designation.starty)/10; y >= (designation.endy)/10;y--)
					{
						for (int x = (designation.startx)/10; x >= (designation.endx)/10;x--)
						{
							//setTiles(x,y,type);
							main.taskManager.addTask(new TaskBuildTile(type, x, y));
						}
					}
				}
    		
				else if (designation.starty <= designation.endy && designation.startx >= designation.endx)
				{
					for (int y = (designation.starty)/10; y <= (designation.endy)/10;y++)
					{
						for (int x = (designation.startx)/10; x >= (designation.endx)/10;x--)
						{
							//setTiles(x,y,type);
							main.taskManager.addTask(new TaskBuildTile(type, x, y));
						}
					}
				}
    		
				else if (designation.starty >= designation.endy && designation.startx <= designation.endx)
				{
					for (int y = (designation.starty)/10; y >= (designation.endy)/10;y--)
					{
						for (int x = (designation.startx)/10; x <= (designation.endx)/10;x++)
						{
							//setTiles(x,y,type);
							main.taskManager.addTask(new TaskBuildTile(type, x, y));
						}
					}
				}
    			
			}
    	}
		else if (Designation.designationArea *type.getVal() > Resources.getResources())
		{
			Resources.setRed();
		}
		designation.setDesignating(false);
	}
	public void draw(Graphics g)
	{
		for(int y = 0;y < dimensiony; y++)
		for (int x = 0; x < dimensionx; x++)
		{
			tiles[x][y].draw(g);
		}	
	}
	
	public void setTiles(int x,int y,tileType type)
	{
			if(type.getSolid() || type == tileType.Space)
			{
				Object obj=objectmanager.getObject(x*10, y*10);
				if(obj != null)		Resources.setResources(obj.type.getValue()+Resources.getResources());
				objectmanager.deleteObject(obj);
			}
			tiles[x][y].settype(type);
	}
	
	public void updateOxygen(Object[] objects)
	{
		for (int y = 1;y<dimensiony-1;y++)
		{
			for(int x = 1;x<dimensionx-1;x++)
			{
				if(!tiles[x][y].getType().getSolid() && !tiles[x][y].hasAirlock)
				{
					for(int cy = -1;cy<=1;cy++)
					{							
						for(int cx = -1;cx<=1;cx++)
						{
							if(tiles[x][y].getOxygen()<tiles[x+cx][y+cy].getOxygen())
							{
								double dif = tiles[x+cx][y+cy].getOxygen()-tiles[x][y].getOxygen();
								tiles[x+cx][y+cy].changeOxygen(-dif/2);
								tiles[x][y].changeOxygen(dif/2);
								if (tiles[x][y].getType() == tileType.Space)
									tiles[x][y].setOxygen(tiles[x][y].getOxygen()/1.3);	
							}
						}
					}
					if (tiles[x][y].hasPerson && tiles[x][y].getOxygen() > 10)
					{
						tiles[x][y].setOxygen(tiles[x][y].getOxygen() - .3);
					}
				}
			}
		}
	}
}

	
	

